package sdk.webview.fmc.com.fmcsdk.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.Button;

import sdk.webview.fmc.com.fmcsdk.bean.SystemSetting;
import sdk.webview.fmc.com.fmcsdk.util.Constant;
import sdk.webview.fmc.com.fmcsdk.util.DesUtil;
import sdk.webview.fmc.com.fmcsdk.util.PreferencesHelper;
import sdk.webview.fmc.com.fmcsdk.util.Utility;

/** 可以切换主题色的Button
 * Created by ydm on 2018/4/12.
 */

@SuppressLint("AppCompatCustomView")
public class LoginFrameStyleColorButton extends Button {
    private int[] colors;
    private Context  context;
    public LoginFrameStyleColorButton(Context context) {
        super(context);
        this.context = context;
    }

    public LoginFrameStyleColorButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public LoginFrameStyleColorButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }
    @Override
    protected void onAttachedToWindow() {
        GradientDrawable bg;
        SystemSetting systemSetting = PreferencesHelper.getData(SystemSetting.class);
        //如果没有系统设置数据，使用默认的颜色
        if (systemSetting==null){
           String appName = (String) Utility.getMetaData(context, Constant.APP_NAME);
            if (appName.equals(Constant.APP_NAME_XIYOU)){
                colors = new int[]{0XFFEC6602, 0XFFEC6602};
            }else {
                colors = new int[]{0XFFDBDEE5, 0XFF989EAA};
            }
            bg = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,colors);
        }else {
            //设置渐变色
            String mobile_title_left_color = systemSetting.getRecord().getMOBILE_TITLE_LEFT_COLOR();
            String mobile_title_right_color = systemSetting.getRecord().getMOBILE_TITLE_RIGHT_COLOR();
            if (!TextUtils.isEmpty(mobile_title_left_color)&&!TextUtils.isEmpty(mobile_title_right_color)){
                colors = new int[]{Color.parseColor(mobile_title_left_color), Color.parseColor(mobile_title_right_color)};
            }else {
                colors = new int[]{0XFFDBDEE5, 0XFF989EAA};
            }
            bg = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,colors);
        }
        int sdk = android.os.Build.VERSION.SDK_INT;
        if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
            setBackgroundDrawable(bg);
        } else {
            setBackground(bg);
        }
        bg.setCornerRadius(DesUtil.dp2px(8));
        super.onAttachedToWindow();
    }
}